#include "stdafx.h"
#include "SomeContainer.h"
#include "IObject.h"
#include "SomeContainterIterator.h"

CSomeContainer::CSomeContainer()
{
}

CSomeContainer::~CSomeContainer()
{
}

bool CSomeContainer::Register(int objectId, boost::shared_ptr<IObject> object)
{
    boost::lock_guard<boost::mutex> autoLock(m_lock);
    Objects_mt::iterator iter = m_map.find(objectId);
    if (iter == m_map.end())
    {
        m_map.insert(std::make_pair(objectId, object));
        return true;
    }
    return false;
}

bool CSomeContainer::Unregister(int objectId)
{
    boost::lock_guard<boost::mutex> autoLock(m_lock);

    Objects_mt::iterator iter = m_map.find(objectId);
    if (iter != m_map.end())
    {
        m_map.erase(iter);
        return true;
    }
    return false;
}

boost::shared_ptr<IObject> CSomeContainer::Query(int objectId)
{
    boost::lock_guard<boost::mutex> autoLock(m_lock);
    Objects_mt::const_iterator iter = m_map.find(objectId);
    if (iter == m_map.end())
    {
        IObject* p = NULL;
        return boost::shared_ptr<IObject>(p);
    }
    return iter->second;
}

std::auto_ptr<ISomeContainterIterator> CSomeContainer::CreateIterator()
{
    return std::auto_ptr<ISomeContainterIterator>(new CSomeContainterIterator(this));
}